package datastructure.tree;

/**
 * 树的打印
 *
 * @author jack.wu
 * @version 1.0
 * @date 2020-03-27
 */
public class BinaryTreeDemo {

    public static void main(String[] args) {
        HeroNode root = new HeroNode(1, "宋江");
        HeroNode node1 = new HeroNode(2, "李白");
        HeroNode node2 = new HeroNode(3, "公孙离");
        HeroNode node3 = new HeroNode(4, "阿珂");
        HeroNode node4 = new HeroNode(5, "高渐离");

        BinaryTree tree = new BinaryTree();
        tree.setRoot(root);
        root.setLeft(node1);
        root.setRight(node2);
        node2.setLeft(node3);
        node2.setRight(node4);

        System.out.println("前序打印--------");
        tree.preOrder();
        System.out.println("中序打印---------");
        tree.midOrder();
        System.out.println("后序打印---------");
        tree.postOrder();

        System.out.println("前序查找~~~~~~~~~~~~~~~~~");
        HeroNode heroNode3 = tree.preOrderSearch(3);
        System.out.println(heroNode3);

        System.out.println("中序查找~~~~~~~~~~~~~~~~~");
        HeroNode heroNode4 = tree.midOrderSearch(4);
        System.out.println(heroNode4);

        System.out.println("后序查找~~~~~~~~~~~~~~~~~");
        HeroNode heroNode5 = tree.midOrderSearch(5);
        System.out.println(heroNode5);

        System.out.println("删除后中序打印");
        tree.deleteById(4);
        tree.midOrder();
    }

}

/**
 * 二叉树
 */
class BinaryTree {

    private HeroNode root;

    public void setRoot(HeroNode root) {
        this.root = root;
    }

    /**
     * 前序遍历
     */
    public void preOrder() {
        if (this.root != null) {
            this.root.preOrder();
        } else {
            System.out.println("二叉树为空~~~~~");
        }
    }

    /**
     * 中序遍历
     */
    public void midOrder() {
        if (this.root != null) {
            this.root.midOrder();
        } else {
            System.out.println("二叉树为空~~~~~");
        }
    }

    /**
     * 后续遍历
     */
    public void postOrder() {
        if (this.root != null) {
            this.root.postOrder();
        } else {
            System.out.println("二叉树为空~~~~~");
        }
    }

    /**
     * 前序查找
     */
    public HeroNode preOrderSearch(int id) {
        return root.postOrderSearch(id);
    }

    /**
     * 中序查找
     */
    public HeroNode midOrderSearch(int id) {
        return root.midOrderSearch(id);
    }

    /**
     * 后续查找
     */
    public HeroNode postOrderSearch(int id) {
        return root.postOrderSearch(id);
    }

    /**
     * 通过id删除
     * @param id
     */
    public void deleteById(int id){
        if (root == null){
            System.out.println("二叉树为空~~~~");
            return;
        }
        if (root.getId() == id){
            // root结点就是要删除结点
            root = null;
            return;
        }
        root.delNode(id);
    }
}

/**
 * 结点
 */
class HeroNode {
    private int id;
    private String name;
    private HeroNode left;
    private HeroNode right;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public HeroNode getLeft() {
        return left;
    }

    public void setLeft(HeroNode left) {
        this.left = left;
    }

    public HeroNode getRight() {
        return right;
    }

    public void setRight(HeroNode right) {
        this.right = right;
    }

    public HeroNode(int id, String name) {
        this.id = id;
        this.name = name;
    }

    @Override
    public String toString() {
        return "HeroNode{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }

    /**
     * 前序遍历
     */
    public void preOrder() {
        System.out.println(this);
        if (this.left != null) {
            // 当前结点左子树不为空 继续遍历
            this.left.preOrder();
        }
        if (this.right != null) {
            // 当前结点右子树不为空 继续遍历
            this.right.preOrder();
        }
    }

    /**
     * 中序遍历
     */
    public void midOrder() {
        if (this.left != null) {
            // 当前结点左子树不为空 继续遍历
            this.left.midOrder();
        }
        System.out.println(this);
        if (this.right != null) {
            // 当前结点右子树不为空 继续遍历
            this.right.midOrder();
        }
    }

    /**
     * 后续遍历
     */
    public void postOrder() {
        if (this.left != null) {
            // 当前结点左子树不为空 继续遍历
            this.left.postOrder();
        }
        if (this.right != null) {
            // 当前结点右子树不为空 继续遍历
            this.right.postOrder();
        }
        System.out.println(this);
    }

    /**
     * 前序查找
     */
    public HeroNode preOrderSearch(int id) {
        if (this.id == id) {
            return this;
        }
        HeroNode resNode = null;
        if (this.left != null) {
            // 当前结点左子树不为空 继续遍历
            resNode = left.preOrderSearch(id);
        }
        if (resNode != null) {
            return resNode;
        }
        if (this.right != null) {
            // 当前结点右子树不为空 继续遍历
            resNode = right.preOrderSearch(id);
        }
        return resNode;
    }

    /**
     * 中序查找
     */
    public HeroNode midOrderSearch(int id) {

        HeroNode resNode = null;
        if (this.left != null) {
            // 当前结点左子树不为空 继续遍历
            resNode = left.midOrderSearch(id);
        }
        if (resNode != null) {
            return resNode;
        }
        if (this.id == id) {
            return this;
        }
        if (this.right != null) {
            // 当前结点右子树不为空 继续遍历
            resNode = right.midOrderSearch(id);
        }
        return resNode;
    }

    /**
     * 后序查找
     */
    public HeroNode postOrderSearch(int id) {

        HeroNode resNode = null;
        if (this.left != null) {
            // 当前结点左子树不为空 继续遍历
            resNode = left.postOrderSearch(id);
        }
        if (resNode != null) {
            return resNode;
        }
        if (this.right != null) {
            // 当前结点右子树不为空 继续遍历
            resNode = right.postOrderSearch(id);
        }
        if (this.id == id) {
            return this;
        }
        return resNode;
    }

    /**
     * 根据id删除
     * @param id
     */
    public void delNode(int id) {

        if (this.left != null && this.left.getId() == id){
            // 左子树就是要删除的
            this.left = null;
            return;
        }
        if (this.right != null && this.right.getId() == id){
            // 右子树就是要删除的
            this.right = null;
            return;
        }
        if (this.left != null){
            // 左递归
            this.left.delNode(id);
        }
        if (this.right != null){
            // 右递归
            this.right.delNode(id);
        }
    }

}